﻿using System.Data;
using SharpObjects.Data;

namespace Authorization
{
    public class AccessControlAdmin : AccessControlReader
    {
        public void CreateResource(string name)
        {
            string queryTemplate = this.sql[SqlTags.insert_resource];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate, new DbParameterDescriptor("name", eDataType.UnicodeString, ParameterDirection.Input, 0, name));
            this.db.ExecuteNonQuery(command);
        }

        public void CreateAclEntryByResourceId(int resourceId, string accessorName, bool denyAccess)
        {
            string queryTemplate = this.sql[SqlTags.insert_acl_entry_by_resource_id];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate,
                new DbParameterDescriptor("resource_id", eDataType.Int, ParameterDirection.Input, 0, resourceId),
                new DbParameterDescriptor("accessor_name", eDataType.UnicodeString, ParameterDirection.Input, 0, accessorName),
                new DbParameterDescriptor("deny_access", eDataType.Boolean, ParameterDirection.Input, 0, denyAccess)
                );
            this.db.ExecuteNonQuery(command);
        }

        public void CreateAclEntryByResourceName(string resourceName, string accessorName, bool denyAccess)
        {
            string queryTemplate = this.sql[SqlTags.insert_acl_entry_by_resource_name];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate,
                new DbParameterDescriptor("resource_id", eDataType.UnicodeString, ParameterDirection.Input, 0, resourceName),
                new DbParameterDescriptor("accessor_name", eDataType.UnicodeString, ParameterDirection.Input, 0, accessorName),
                new DbParameterDescriptor("deny_access", eDataType.Boolean, ParameterDirection.Input, 0, denyAccess)
                );
            this.db.ExecuteNonQuery(command);
        }

        public void DeleteAllAclEntriesByResourceId(int resourceId)
        {
            string queryTemplate = this.sql[SqlTags.delete_all_acl_entries_by_resource_id];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate, new DbParameterDescriptor("resorce_id", eDataType.Int, ParameterDirection.Input, 0, resourceId));
            this.db.ExecuteNonQuery(command);
        }

        public void DeleteAllAclEntriesByResourceName(string resourceName)
        {
            string queryTemplate = this.sql[SqlTags.delete_all_acl_entries_by_resource_name];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate, new DbParameterDescriptor("name", eDataType.UnicodeString, ParameterDirection.Input, 0, resourceName));
            this.db.ExecuteNonQuery(command);
        }

        public void DeleteAclEntry(int resourceId, string accessorName)
        {
            string queryTemplate = this.sql[SqlTags.delete_acl_entry];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate,
                new DbParameterDescriptor("resource_id", eDataType.Int, ParameterDirection.Input, 0, resourceId),
                new DbParameterDescriptor("accessor_name", eDataType.UnicodeString, ParameterDirection.Input, 0, accessorName)
                );
            this.db.ExecuteNonQuery(command);
        }

        public void DeleteResourceByResourceId(int resourceId)
        {
            string queryTemplate = this.sql[SqlTags.delete_resource_by_id];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate, new DbParameterDescriptor("id", eDataType.Int, ParameterDirection.Input, 0, resourceId));
            this.db.ExecuteNonQuery(command);
        }

        public void DeleteResourceByResourceName(string resourceName)
        {
            string queryTemplate = this.sql[SqlTags.delete_resource_by_name];
            IDbCommand command = this.db.CreateQueryCommand(queryTemplate, new DbParameterDescriptor("name", eDataType.UnicodeString, ParameterDirection.Input, 0, resourceName));
            this.db.ExecuteNonQuery(command);
        }
    }
}